package router

import (
	"github.com/gin-gonic/gin"
	"kf/controller"
	"kf/middleware"
	"kf/ws"
)

type Api struct {
	IpBlack             *middleware.IpBlack
	Rbac                *middleware.Rbac
	LoggerSave          *middleware.LoggerSave
	MessageController   *controller.MessageController
	LoginController     *controller.LoginController
	LogController       *controller.LogController
	DeptController      *controller.DeptController
	SysUserController   *controller.SysUserController
	SysDictController   *controller.SysDictController
	MainController      *controller.MainController
	KefuController      *controller.KefuController
	VisitorController   *controller.VisitorController
	AboutController     *controller.AboutController
	SysRoleController   *controller.SysRoleController
	NoticeController    *controller.NoticeController
	IpController        *controller.IpController
	ConfigController    *controller.ConfigController
	AutoReplyController *controller.SysAutoReplyController
	ReplyController     *controller.ReplyController
	ChartController     *controller.ChartController
	MenusController     *controller.MenusController
	SysRuleController   *controller.SysRuleController
	WsUser              *ws.WsUser
	WsVisitor           *ws.WsVisitor
}

func (api *Api) InitApiRouter(engine *gin.Engine) {
	//路由分组
	v2 := engine.Group("/2")
	{
		//获取消息
		//v2.GET("/messages", api.MessageController.GetMessagesV2)
		//发送单条信息
		v2.POST("/message", api.IpBlack.Ipblack, api.MessageController.SendMessageV2)
		//关闭连接
		v2.GET("/message_close", api.MessageController.SendCloseMessageV2)
		//分页查询聊天记录
		v2.GET("/messagesPages", api.MessageController.GetMessagesPage)
		//绑定
		//v2.POST("/bindOfficial", controller.PostBindOfficial)
	}
	//聊天记录
	MessageV1 := engine.Group("/api/v1/message")
	{
		//分页查询消息
		MessageV1.GET("/pageByVisitor", api.MessageController.GetMessagesPageByVisitor)
		//获取消息
		MessageV1.GET("/page", middleware.JwtApiMiddleware, api.MessageController.GetMessagesPage)
		//客户的消息已读
		MessageV1.GET("/readMessage/:visitorId", middleware.JwtApiMiddleware, api.MessageController.ReadMessage)
	}

	VisitorV1 := engine.Group("/visitor")
	{
		VisitorV1.GET("", middleware.JwtApiMiddleware, api.VisitorController.GetVisitor)
		VisitorV1.GET("/page", middleware.JwtApiMiddleware, api.VisitorController.GetVisitorPage)
		VisitorV1.PUT("/delete/bind/:visitorId",
			middleware.JwtApiMiddleware,
			api.Rbac.RbacAuth1("visitor:delete:bind"),
			api.VisitorController.DeleteBind)
	}

	v1 := engine.Group("/api/v1")
	{
		v1.GET("/auth/captcha", controller.GetCaptchaV1)
		v1.POST("/auth/login", api.LoginController.LoginCheckPassV1)
		//退出登陆
		v1.DELETE("/auth/logout", middleware.JwtApiMiddleware, api.LoginController.Logout)
		//上传文件
		v1.POST("/files", api.IpBlack.Ipblack, middleware.JwtApiMiddleware, controller.UploadFile)
	}

	Logv1 := engine.Group("/api/v1/logs")
	{
		Logv1.GET("/page", middleware.JwtApiMiddleware, api.LogController.ListPagedLogs)
	}

	Deptv1 := engine.Group("/api/v1/dept")
	{
		//获取部门列表
		Deptv1.GET("", middleware.JwtApiMiddleware, api.DeptController.GetDepts)
		//获取部门
		Deptv1.GET("/options", middleware.JwtApiMiddleware, api.DeptController.DeptOptions)
		//查找
		Deptv1.GET("/:deptId/form", middleware.JwtApiMiddleware, api.DeptController.GetDeptForm)
		//新建
		Deptv1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dept:add"), api.DeptController.SaveDept)
		//编辑
		Deptv1.PUT("/:id", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dept:edit"), api.DeptController.UpdateDept)
		//删除
		Deptv1.DELETE("/:ids", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dept:delete"), api.DeptController.DeleteDepts)
	}

	Dictv1 := engine.Group("/api/v1/dict")
	{
		//字典
		Dictv1.GET("/:code/options", middleware.JwtApiMiddleware, api.SysDictController.GetDictOptions)
		//字典分页
		Dictv1.GET("/page", middleware.JwtApiMiddleware, api.SysDictController.GetDictPage)
		//字典表单
		Dictv1.GET("/form/:dictId", middleware.JwtApiMiddleware, api.SysDictController.GetDictForm)
		//保存
		Dictv1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dict:add"), api.SysDictController.SaveDict)
		//修改字典
		Dictv1.PUT("/:dictId", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dict:edit"), api.SysDictController.UpdateDict)
		//删除字典
		Dictv1.DELETE("/:dictIds", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:dict:delete"), api.SysDictController.DeleteDict)
	}

	MenusV1 := engine.Group("/api/v1/menus")
	{
		//获取菜单
		MenusV1.GET("", middleware.JwtApiMiddleware, api.MenusController.GetMenus)
		//获取路由
		MenusV1.GET("/routes", middleware.JwtApiMiddleware, api.MenusController.RoutesV1)
		//菜单下拉列表
		MenusV1.GET("/options", middleware.JwtApiMiddleware, api.MenusController.ListMenuOptions)
		//查找
		MenusV1.GET("/:menuId/form", middleware.JwtApiMiddleware, api.MenusController.GetMenuForm)
		//编辑
		MenusV1.PUT("/:id", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:menu:edit"), api.MenusController.UpdateMenu)
		//新增菜单
		MenusV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:menu:add"), api.MenusController.UpdateMenu)
		//删除
		MenusV1.DELETE("/:id", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:menu:delete"), api.MenusController.DeleteMenu)
	}

	RoleV1 := engine.Group("/api/v1/roles")
	{
		//角色
		RoleV1.GET("/options", middleware.JwtApiMiddleware, api.SysRoleController.GetRolesOptions)
		//获取分页 'LOGIN','USER','ROLE','DEPT','MENU','DICT','OTHER'
		RoleV1.GET("/page",
			middleware.JwtApiMiddleware,
			api.LoggerSave.Logger(3, "获取角色分页"),
			api.SysRoleController.GetPage)
		//查找
		RoleV1.GET("/:roleId/form", middleware.JwtApiMiddleware, api.SysRoleController.GetRoleForm)
		//权限分配
		RoleV1.GET("/menuIds/:roleId", middleware.JwtApiMiddleware, api.SysRoleController.GetRoleMenuIds)
		//分配菜单(包括按钮权限)给角色
		RoleV1.PUT("/menus/:roleId", middleware.JwtApiMiddleware, api.SysRoleController.AssignMenusToRole)
		//新增角色
		RoleV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:role:add"), api.SysRoleController.AddRole)
		//编辑
		RoleV1.PUT("/:id", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:role:edit"), api.SysRoleController.UpdateRole)
		//删除
		RoleV1.DELETE("/:ids", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:role:delete"), api.SysRoleController.DeleteRole)
	}

	UserV1 := engine.Group("/api/v1/users")
	{
		//获取自己的信息
		UserV1.GET("/me", middleware.JwtApiMiddleware, api.SysUserController.MeV1)
		//分页获取user
		UserV1.GET("/page", middleware.JwtApiMiddleware, api.SysUserController.Page)
		//查找
		UserV1.GET("/form/:userId", middleware.JwtApiMiddleware, api.SysUserController.GetUserForm)
		//查询客服id和名称
		UserV1.GET("/option", middleware.JwtApiMiddleware, api.SysUserController.Option)
		//修改用户密码
		UserV1.PATCH("/password/:userId", middleware.JwtApiMiddleware,
			api.Rbac.RbacAuth1("sys:user:password:reset"),
			api.SysUserController.UpdatePassword)
		//新建用户
		UserV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth1("sys:user:add"), api.SysUserController.SaveUser)
		//修改用户
		UserV1.PUT("/:userId", middleware.JwtApiMiddleware,
			api.Rbac.RbacAuth1("sys:user:edit"), api.SysUserController.UpdateUser)
		//删除用户
		UserV1.DELETE("/:ids", middleware.JwtApiMiddleware,
			api.Rbac.RbacAuth1("sys:user:delete"), api.SysUserController.DeleteUsers)
	}

	BlackV1 := engine.Group("/ipblack")
	{
		//创建黑名单
		BlackV1.POST("", middleware.JwtApiMiddleware, api.IpBlack.Ipblack, api.IpController.PostIpBlack)
		//删除黑名单
		BlackV1.DELETE("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.IpController.DelIpBlack)
		//获取所有黑名单
		BlackV1.GET("/all", middleware.JwtApiMiddleware, api.IpController.GetIpBlacks)
		//根据当前用户获取黑名单
		BlackV1.GET("", middleware.JwtApiMiddleware, api.IpController.GetIpblacksByKefuId)
		//分页获取黑名单
		BlackV1.GET("/page", middleware.JwtApiMiddleware, api.IpController.Page)
	}

	RuleV1 := engine.Group("/rule")
	{
		//保存规则
		RuleV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.SysRuleController.SaveRule)
		//删除规则
		RuleV1.DELETE("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.SysRuleController.DeleteRule)
		//保存规则
		RuleV1.PUT("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.SysRuleController.UpdateRule)
		//获取规则
		RuleV1.POST("/one", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.SysRuleController.GetRule)
		//分页
		RuleV1.GET("/page", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.SysRuleController.GetRulePage)
	}

	ConfigV1 := engine.Group("/config")
	{
		ConfigV1.GET("/all", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ConfigController.GetConfigs)
		ConfigV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ConfigController.PostConfig)
		ConfigV1.GET("", api.ConfigController.GetConfig)
	}
	AutoReplyV1 := engine.Group("/auto/reply")
	{
		//分页查询
		AutoReplyV1.GET("/page", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.AutoReplyController.GetAutoReplyPage)
		//
		AutoReplyV1.POST("", middleware.JwtApiMiddleware, api.Rbac.RbacAuth,
			api.AutoReplyController.SaveOrUpdateAutoReply)
		AutoReplyV1.DELETE("/:id", api.AutoReplyController.DeleteAutoReply)
	}

	//前后聊天
	engine.GET("/ws_kefu", middleware.JwtApiMiddleware, api.WsUser.NewKefuServer)
	//客户ws链接创建
	engine.GET("/ws_visitor", api.IpBlack.Ipblack, api.WsVisitor.NewVisitorServer)

	engine.GET("/messages", api.VisitorController.GetVisitorMessage)
	engine.GET("/message_notice", api.MessageController.SendVisitorNotice)
	//上传文件
	engine.POST("/uploadimg", api.IpBlack.Ipblack, controller.UploadImg)
	//上传文件
	engine.POST("/uploadfile", api.IpBlack.Ipblack, controller.UploadFile)
	//获取未读消息数
	engine.GET("/message_status", api.VisitorController.GetVisitorMessage)
	//设置消息已读
	engine.POST("/message_status", api.VisitorController.GetVisitorMessage)

	//获取客服信息
	engine.POST("/kefuinfo_client", middleware.JwtApiMiddleware, api.KefuController.PostKefuClient)

	engine.GET("/kefuinfo_setting", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.KefuController.GetKefuInfoSetting)

	engine.GET("/other_kefulist", middleware.JwtApiMiddleware, api.KefuController.GetOtherKefuList)
	engine.GET("/trans_kefu", middleware.JwtApiMiddleware, api.KefuController.PostTransKefu)

	engine.GET("/visitors_online", api.VisitorController.GetVisitorOnlines)
	//获取在线的客户
	engine.GET("/visitors_kefu_online", middleware.JwtApiMiddleware, api.VisitorController.GetKefusVisitorOnlines)
	//删除对应ip的链接
	engine.GET("/clear_online_tcp", controller.DeleteOnlineTcp)
	//初始化客户的连接
	engine.POST("/visitor_login", api.IpBlack.Ipblack, api.VisitorController.PostVisitorLogin)

	engine.GET("/statistics", middleware.JwtApiMiddleware, api.MainController.GetStatistics)
	//前台接口
	engine.GET("/about", api.AboutController.GetAbout)
	engine.POST("/about", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.AboutController.PostAbout)
	engine.GET("/aboutpages", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.AboutController.GetAbouts)
	engine.GET("/notice", api.NoticeController.GetNotice)
	engine.GET("/autoreply", api.ReplyController.GetAutoReplys)
	//快捷回复
	engine.GET("/replys", middleware.JwtApiMiddleware, api.ReplyController.GetReplys)
	engine.POST("/reply", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ReplyController.PostReply)
	engine.POST("/reply_content", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ReplyController.PostReplyContent)
	engine.POST("/reply_content_save", middleware.JwtApiMiddleware, api.ReplyController.PostReplyContentSave)
	engine.DELETE("/reply_content", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ReplyController.DelReplyContent)
	engine.DELETE("/reply", middleware.JwtApiMiddleware, api.Rbac.RbacAuth, api.ReplyController.DelReplyGroup)
	engine.POST("/reply_search", middleware.JwtApiMiddleware, api.ReplyController.PostReplySearch)
	//客服路由分组
	kefuGroup := engine.Group("/kefu")
	kefuGroup.Use(middleware.JwtApiMiddleware)
	{
		kefuGroup.GET("/chartStatistics", api.ChartController.GetChartStatistic)
	}
}
